#include "board.h"

char buf[50];
char *name_tcp;
int num_tcp;
int connfd;

void *running(void *arg)
{
    printf("%dn\n",__LINE__);
    list head = init_list();
    char buff[50];
    printf("%dn\n",__LINE__);

    while(1)
    {
        usleep(50*1000);

        // printf("%dn\n",__LINE__);
        if(strlen(buf) != 0 && num_tcp == 1)//添加患者
        {
            if(add_node(head, new_node(num_tcp, name_tcp)) == false)
            {
                printf("%dn\n",__LINE__);
                strcpy(buff, "人已满");
                send(connfd, buff, sizeof(buff), 0);
                printf("人已满\n");
            }
            show(head);
            memset(buf,  0, sizeof(buf));
        }
        else if(strlen(buf) != 0 && (num_tcp == 2 || num_tcp == 3))//叫下一位||过号
        {
            pthread_mutex_lock( &m );
            if(del_node(head) == false)
            {
                printf("%dn\n",__LINE__);
                strcpy(buff, "已无人");
                send(connfd, buff, sizeof(buff), 0);                                
                printf("已无人\n");
            }
            pthread_mutex_unlock( &m );
            show(head);
            memset(buf,  0, sizeof(buf));            
        }
        else if(strlen(buf) != 0 && num_tcp == 4)//设置时间
        {
            strcpy(sj, name_tcp);
            memset(buf,  0, sizeof(buf));  
        }
        // printf("%dn\n",__LINE__);

        memset(buff, 0, sizeof(buff));

        pthread_mutex_lock( &m );
        if(head->size >= 0)
        strcpy(zz,  head->prve->name);
        if(head->size >= 1)
        strcpy(dd1, head->prve->prve->name);
        if(head->size >= 2)
        strcpy(dd2, head->prve->prve->prve->name);
        if(head->size >= 3)
        strcpy(dd3, head->prve->prve->prve->prve->name);
        if(head->size >= 4)
        strcpy(dd4, head->prve->prve->prve->prve->prve->name);
        if(head->size >= 5)
        strcpy(dd5, head->prve->prve->prve->prve->prve->prve->name);
        if(head->size >= 6)
        strcpy(dd6, head->prve->prve->prve->prve->prve->prve->prve->name);
        pthread_mutex_unlock( &m );

        // printf("%dn\n",__LINE__);
    }
}

void *board_TCP(void *arg)
{
    // 创建TCP
    int sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    printf("%dn\n",__LINE__);
    // 准备好开发板的地址
    struct sockaddr_in addr;
    socklen_t len = sizeof(addr);
    bzero(&addr, len);
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    addr.sin_port = htons(50001);

    int on = 1;
    setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));

    printf("%dn\n",__LINE__);

    // 绑定sockfd和地址
    if(bind(sockfd, (struct sockaddr *)&addr, len) == -1)
    {
        perror("绑定地址失败\n");
        exit(0);
    }

    printf("%dn\n",__LINE__);
    // 设定监听状态
    listen(sockfd, 3);
    printf("%dn\n",__LINE__);
    while(1)
    {
        // 等待客户端的请求
        struct sockaddr_in peeraddr;
        len = sizeof(peeraddr);
        bzero(&peeraddr, len);
        connfd = accept(sockfd, (struct sockaddr *)&peeraddr, &len);
        printf("%dn\n",__LINE__);
        if(connfd == -1)
        {
            perror("connfd 失败");
            exit(0);
        }
        else
        {
            printf("accept 成功\n");      
            printf("%dn\n",__LINE__);

            memset(buf, 0, sizeof(buf)); 
            while(1)
            {

                int n = read(connfd, buf, 50);
                if(n == -1)
                {
                    perror("read failed");
                    break;
                }
                else if(n == 0)
                {
                    printf("连接断开\n");
                    break;                
                }
                else//读取数据
                {
                    printf("%dn\n",__LINE__);
                    printf("%s\n", buf);  
                    char *delim = "-";
                    sscanf(buf, "%d-", &num_tcp);//剪切出数字
                    name_tcp = strstr(buf, delim) + 1;//剪切出姓名
                    printf("%dn\n",__LINE__);
                    // printf("序号:%d\t姓名:%s\n",num_tcp, name_tcp);
                    printf("%dn\n",__LINE__);
                }
            }        
        }        
    }
}

void handle(int i, char *h)
{
    if(i == 1)
    {
        printf("%d\t%s\n", i, h);
    }
}

list init_list(void)//初始链表头节点
{
    list new = calloc(1,sizeof(list1));
    if(new != NULL)
    {
        new->next = new;
        new->prve = new;

        // strcpy(new->name,  "王大大");
    }
    new->size = 0;
    return new;
}

list new_node(int num_tcp, char *name_tcp)//创建新节点
{
    list new = calloc(1, sizeof(list1));
    if(new != NULL)
    {
        new->next = new;
        new->prve = new;
    }
    strcpy(new->name, name_tcp);
    return new;
}

bool is_full(list head)
{
    return head->size == 7;
}

bool add_node(list head, list new)//添加节点
{
    if(is_full(head))
    {
        return false;
    }

    new->prve = head;
    new->next = head->next;

    head->next = new;
    new->next->prve = new;

    head->size++;

    return true;
}


bool is_empty(list head)//判断节点个数
{
    return head->size == 0;
}

bool del_node(list head)//删除最后节点
{
    if(is_empty(head))
        return false;

    list p = head->prve;

    if(num_tcp == 3)
    {
        strcat(gh, " ");
        strcat(gh, p->name);
    }
    // pthread_mutex_lock( &m );
    memset(p->name, 0, sizeof(p->name));
    // pthread_mutex_unlock( &m );
    // usleep(500*1000);

    p->prve->next = head;
    head->prve = p->prve;
    head->size--;
    free(p);

    return true;
}

void show(list head)//遍历输出信息
{
    for(list p = head->next; p != head; p = p->next)
    {
        printf("姓名%s\n", p->name);
    }
}